/*
 * File Name：MPBaseActivity.java
 * Copyright：Copyright 2008-2014 CiWong.Inc. All Rights Reserved.
 * Description： MPBaseActivity.java
 * Modify By：bin
 * Modify Date：2014年4月7日
 * Modify Type：Add
 */
package com.ciwong.mobilepay.ui;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.Toast;

import com.ciwong.libs.widget.CWToast;
import com.ciwong.libs.widget.MarqueeTextView;
import com.ciwong.mobilepay.i.GoBackListener;
import com.ciwong.mobilepay.view.TitleBar;

/**
 * 移动支付模块Activity基类
 * 
 * @author bin
 * @version ciwong v.1.0 2014年4月7日
 * @since ciwong v.1.0
 */
public abstract class MPBaseActivity extends Activity
{
    /**
     * Intent键值,存储标题栏的返回按钮显示字符
     */
    public static final String GO_BACK = "GO_BACK";

    /**
     * 显示返回按钮
     */
    public static final int SHOW_GOBACK_ID = 1;

    /**
     * 隐藏返回按钮
     */
    public static final int HIDE_GOBACK_ID = -2;

    /**
     * 是否禁用标题栏
     */
    protected boolean fororbiddenTitleBar;

    /**
     * 标题栏右侧Button
     */
    protected Button goBack;

    /**
     * 标题栏中间TextView
     */
    protected MarqueeTextView title;

    private GoBackListener mGoBackListener;

    private CharSequence titleBarText;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        preCreate();
        super.onCreate(savedInstanceState);
        setContentView(setView());
        if (!fororbiddenTitleBar)
        {
            baseFindViews();
            baseInitEvent();
            baseInit();
        }
        findViews();
        initEvent();
        init();
        loadData();
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
    }

    /**
     * 预创建,运行在{@link #onCreate(Bundle)}前
     * 
     */
    public void preCreate()
    {

    }

    @Override
    protected void onStart()
    {
        super.onStart();
    }

    /**
     * 设置内容
     * 
     * @return 返回一个View
     */
    protected abstract View setView();

    /**
     * 查找元素(控件)
     */
    protected abstract void findViews();

    /**
     * 初始化操作(如初始化事件)
     * 
     */
    protected abstract void initEvent();

    /**
     * 初始化(赋值、显示、隐藏等)
     */
    protected abstract void init();

    /**
     * 加载数据
     */
    protected abstract void loadData();

    /**
     * 隐藏软件盘
     * 
     * @param view
     *            视图
     */
    protected void hideSoftInput(View view)
    {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }

    private void baseFindViews()
    {
        goBack = (Button) findViewById(TitleBar.goBack);
        title = (MarqueeTextView) findViewById(TitleBar.title);
    }

    private void baseInitEvent()
    {
        if (goBack != null)
        {
            goBack.setOnClickListener(new OnClick());
        }
    }

    private void baseInit()
    {
        if ((title == null || goBack == null) && !fororbiddenTitleBar)
        {
            Log.e("debug", "没有引入布局title_bar.xml");
        }
        Intent mIntent = getIntent();
        if (mIntent != null && goBack != null)
        {
            int goBackResId = mIntent.getIntExtra(GO_BACK, SHOW_GOBACK_ID);
            if (goBackResId == HIDE_GOBACK_ID || goBackResId == -1)
            {
                goBack.setVisibility(View.GONE);
            }
            else if (goBackResId != SHOW_GOBACK_ID)
            {
                goBack.setText(goBackResId);
            }
            else
            {
                goBack.setText("返回");
            }
        }
        if (titleBarText != null)
        {
            title.setText(titleBarText);
        }
    }

    /**
     * 
     * 设置标题栏中返回按钮事件,若不设置默认操作是finish当前Activity
     * 
     * @param listener
     *            {@link GoBackListener}
     */
    protected void setGoBackListener(GoBackListener listener)
    {
        this.mGoBackListener = listener;
    }

    /**
     * 
     * 设置标题
     * 
     * @param str
     *            资源ID
     */
    protected void setTitleBarText(String str)
    {
        if (title != null)
        {
            title.setText(str);
        }
        else
        {
            titleBarText = str;
        }
    }

    /**
     * 
     * 属性 fororbiddenTitleBar 的GET方法
     * 
     * @return fororbiddenTitleBar
     */
    public boolean isForbiddenTitleBar()
    {
        return fororbiddenTitleBar;
    }

    /**
     * 
     * 属性 fororbiddenTitleBar 的SET方法
     * 
     * @param isForbiddenTitleBar
     *            isForbiddenTitleBar
     */
    public void setForbiddenTitleBar(boolean isForbiddenTitleBar)
    {
        this.fororbiddenTitleBar = isForbiddenTitleBar;
    }

    /**
     * 标题栏右侧点击事件
     * 
     * @author bin
     * 
     */
    private class OnClick implements OnClickListener
    {

        @Override
        public void onClick(View v)
        {
            if (v.getId() == TitleBar.goBack)
            {
                if (mGoBackListener != null)
                {
                    mGoBackListener.goBack();
                }
                else
                {
                    finish();
                }
            }
        }
    }

    public void showError(int resId)
    {
        CWToast.makeText(this, resId, Toast.LENGTH_LONG, true)
                .setToastType(CWToast.TOAST_TYPE_ERROR).show();
    }

    public void showError(String msg)
    {
        CWToast.makeText(this, msg, Toast.LENGTH_LONG, true)
                .setToastType(CWToast.TOAST_TYPE_ERROR).show();
    }

    public void showSuccess(int resId)
    {
        CWToast.makeText(this, resId, Toast.LENGTH_LONG, true)
                .setToastType(CWToast.TOAST_TYPE_SUC).show();
    }

    public void showSuccess(String msg)
    {
        CWToast.makeText(this, msg, Toast.LENGTH_LONG, true)
                .setToastType(CWToast.TOAST_TYPE_SUC).show();
    }

    public void showAlert(int resId)
    {
        CWToast.makeText(this, resId, Toast.LENGTH_LONG, true)
                .setToastType(CWToast.TOAST_TYPE_ALERT).show();
    }

}
